﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL.POCO;
using DAL.Repositories;
using GameAdmin.Classes;

namespace GameAdmin.Pages
{
    public partial class mapdesigner : System.Web.UI.Page
    {
        private Round _level;
        private IEnumerable<Field> _mapFields;
        private Dictionary<int, string> _fieldTypes;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (RouteData.Values["roundId"] == null) return;

            Page.ClientScript.RegisterClientScriptInclude("javascript_jquery", "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
            Page.ClientScript.RegisterClientScriptInclude("jsonify", "http://localhost:1666/Js/json2.js");

            btnClearAllFields.Click += new EventHandler(btnClearAllFields_Click);

            GetFieldTypes();
            GetLevelDetails();

            if (!IsPostBack)
            {
                DrawMapPlan();
            }
        }

        void btnClearAllFields_Click(object sender, EventArgs e)
        {
            FieldRepository.DeleteFields(_level.id);

            DrawMapPlan();
        }

        #region Init private members

        private void GetFieldTypes()
        {
            _fieldTypes = HelperMethods.GetFieldTypes();
        }

        private void GetLevelDetails()
        {
            lblId.Text = (string)RouteData.Values["roundId"]; 
            literalRoundId.Text = (string)RouteData.Values["roundId"];
            lnkLevelEdit.NavigateUrl = "~/level/" + literalRoundId.Text;
            lnkRevert.NavigateUrl = "~/mapdesigner/" + literalRoundId.Text;
            
            int roundId = Int32.Parse(literalRoundId.Text);
            _level = RoundRepository.GetRound(roundId);
        }

        #endregion

        #region Render map plan

        private void DrawMapPlan()
        {
            _mapFields = FieldRepository.GetFields(_level.id).ToArray();

            for (int y = 0; y < _level.height; y++)
            {
                Panel row = new Panel();
                row.CssClass = "row";
                
                // display row
                for (int x = 0; x < _level.width; x++)
                {
                    if (x == 0 || y == 0 || x == _level.width - 1 || y == _level.height - 1)
                    {
                        // surrounding walls
                        row.Controls.Add(GetSurroundingWall(x, y));
                    }
                    else
                    {
                        // otherwise it's a standard cell
                        row.Controls.Add(GetCell(x, y));
                    }
                }

                pnl.Controls.Add(row);
            }
        }

        private Panel GetCell(int x, int y)
        {
            Field f = (from o in _mapFields
                       where o.x == x && o.y == y
                       select o).SingleOrDefault();

            Panel pnl = new Panel();
            pnl.CssClass = "field";
            pnl.Attributes.Add("coords", x.ToString() + "," + y.ToString());

            if (f == null)
            {
                pnl.Attributes.Add("type", "empty");
            }
            else
            {
                pnl.Attributes.Add("type", _fieldTypes[f.type]);
                if (f.difficulty != null) pnl.Attributes.Add("difficulty", f.difficulty.ToString());
                if (f.radiusGuard != null) pnl.Attributes.Add("radius", f.radiusGuard.ToString());
            }

            return pnl;
        }

        private Panel GetSurroundingWall(int x, int y)
        {
            Panel pnl = new Panel();
            pnl.CssClass = "field";
            pnl.Attributes.Add("coords", x.ToString() + "," + y.ToString());
            pnl.Attributes.Add("type", "wall");
            pnl.Attributes.Add("border", "true");

            return pnl;
        }

        #endregion
    }
}